/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bice.backbean;

import com.bice.entity.User;
import com.bice.service.UserService;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

    
/**
 *
 * @author sirius
 */
@ManagedBean
@ViewScoped
public class ChatBean extends BackBean {
 
    private UserService service;
    
    public ChatBean() {
        super();
    }
    //</editor-fold>
    
    private String message;
    private String log;
    private String nickName;
    private User userTarget;

    public String getMessage() {
        return message;
    }
    
    public String getLog() {
        return log;
    }
    
    public User getUserTarget() {
        return userTarget;
    }
    
    public void setUserTarget(User user) {
        this.userTarget = user;
        log = "";
    }
    
    public void setMessage(String message) {
        this.message = message;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    

    public boolean isLoggedIn() {
        return getSessionBean().isLogged();
    }

    public void send() {
        log += "<b>" + getSessionBean().getUser().getName() + "</b>: " + message + "<br/>";
        message = "";
    }

    public void login() {
        User user = getUserService().getUser(nickName);
        if(user == null) {
            user = new User();
            user.setNickname(nickName);
            getUserService().save(user);
        }
        getSessionBean().setUser(user);
    }
    public void disconnect() {
        getSessionBean().disconnect();
    }
    
    public boolean isSelectedTargetSelected() {
        return userTarget != null;
    }
    
    public List<User> getContacts() {
        return getUserService().getContacts(nickName);
    }
    
    public UserService getUserService() {
        if(service == null) {
            service = (UserService) getSpringBean(UserService.class);
        }
        return  service;
    }
    
}